import { defineStore } from "pinia";

interface PendingMembers {
  id: number;
  username: string;
  pending: string;
  selected: boolean;
}

interface FormalMembers {
  id: number;
  username: string;
  remark: string;
  permission: 'admin' | 'member';
  selected: boolean;
  editRemark: string;
  editPermission: 'admin' | 'member';
}

interface TeamMembers {
  pending: PendingMembers[];
  formal: FormalMembers[];
}

export const useMemberStore = defineStore('member', {
  state: () => ({
    memberByTeam: {} as Record<string, TeamMembers>,
    errorByTeam: {} as Record<string, string | null>
  }),

  getters: {
    getPendingMembers: (state) => (teamID: string) => {
      if (!state.memberByTeam[teamID]) return [];
      return state.memberByTeam[teamID].pending;
    },
    getFormalMembers: (state) => (teamID: string) => {
      if (!state.memberByTeam[teamID]) return [];
      return state.memberByTeam[teamID].formal;
    },
    isAdmin: (state) => (teamID: string, username: string) => {
      const members = state.memberByTeam[teamID]?.formal || [];
      return members.some(mem => mem.username === username && mem.permission === 'admin');
    }
  },

  actions: {
    initTeamMembers(teamId: string) {
      if (!this.memberByTeam[teamId]) {
        this.memberByTeam[teamId] = {
          pending: [],
          formal: []
        };
      }
    },

    setPendingMembers(teamId: string, members: PendingMembers[]) {
      this.initTeamMembers(teamId);
      const target = this.memberByTeam[teamId].pending;
      target.splice(0, target.length, ...members);
    },

    setFormalMembers(teamId: string, members: FormalMembers[]) {
      this.initTeamMembers(teamId);
      const target = this.memberByTeam[teamId].formal;
      target.splice(0, target.length, ...members);
    },

    addPendingMember(teamId: string, member: PendingMembers) {
      this.initTeamMembers(teamId);
      this.memberByTeam[teamId].pending.push(member);
    },

    removePendingMember(teamId: string, memberId: number) {
      const team = this.memberByTeam[teamId];
      if (!team) return;
      team.pending = team.pending.filter(member => member.id !== memberId);
    },

    removeFormalMember(teamId: string, memberId: number) {
      const team = this.memberByTeam[teamId];
      if(!team) return;
      team.formal = team.formal.filter(member => member.id !== memberId);
    },

    updateFormalMember(teamId: string, updatedMember: Partial<FormalMembers> & { id: number }) {
      const team = this.memberByTeam[teamId];
      if (!team) return;
      const member = team.formal.find(m => m.id === updatedMember.id);
      if (member) Object.assign(member, updatedMember);
    },

    updatePendingMember(teamId: string, memberId: number, patch: Partial<PendingMembers>) {
      const team = this.memberByTeam[teamId];
      if (!team) return;
      const member = team.pending.find(m => m.id === memberId);
      if (member) Object.assign(member, patch);
    },

    resetAllSelection(teamId: string) {
      const team = this.memberByTeam[teamId];
      if (!team) return;
      team.pending.forEach(mem => mem.selected = false);
      team.formal.forEach(mem => mem.selected = false);
    },

    clearTeamMembers(teamId: string) {
      delete this.memberByTeam[teamId];
    },

    setError(teamId: string, message: string | null) {
      this.errorByTeam[teamId] = message;
    }
  }
});
